
//
//  ChooseMsgVisibleView.m
//  家校互通
//
//  Created by 李赛 on 15/6/14.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "ChooseMsgVisibleView.h"

@implementation ChooseMsgVisibleView

- (id)initWithFrame:(CGRect)frame withTitleArray:(NSArray*)titleArray andHeaderTitle:(NSString *)headerTitle
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor=[UIColor whiteColor];
        self.layer.masksToBounds=YES;
        self.layer.cornerRadius=15;
        _titleArray=[[NSMutableArray alloc] initWithArray:titleArray];
        _headerTitle=headerTitle;
        _contentTableView=[[UITableView alloc]initWithFrame:CGRectMake(5, 5, frame.size.width-10, frame.size.height-10) style:UITableViewStylePlain];
        _contentTableView.backgroundColor=[UIColor clearColor];
        _contentTableView.delegate=self;
        _contentTableView.dataSource=self;
        _contentTableView.scrollEnabled=NO;
        _contentTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self addSubview:_contentTableView];
        [_contentTableView reloadData];
        
    }
    return self;
}


-(void)presentViewFromVisibleView:(UIView *)view
{
    _maskView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, DSF_SCREENWIDTH, DSF_SCREENHEIGHT)];
    UITapGestureRecognizer *tapScreen=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(disMissCurrentView)];
    [_maskView addGestureRecognizer:tapScreen];
    _maskView.backgroundColor=[UIColor blackColor];
    _maskView.alpha=0;
    
    [view addSubview:_maskView];
     [view addSubview:self];
    self.alpha=0;
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha=1;
        _maskView.alpha=0.6;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)disMissCurrentView
{
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha=0;
        _maskView.alpha=0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [_maskView removeFromSuperview];
    }];
}

#pragma mark UITableViewDelegate--
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)];
    UILabel *titleLB=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,  self.bounds.size.width, 40)];
    
    UIView *seperateView=[[UIView alloc] initWithFrame:CGRectMake(0, 39, self.bounds.size.width, 0.5)];
    seperateView.backgroundColor=[UIColor lightGrayColor];
    seperateView.alpha=0.7;
    [headerView addSubview:seperateView];
    
    [titleLB setFont:[UIFont systemFontOfSize:15]];
    titleLB.text=_headerTitle;
    titleLB.textAlignment=NSTextAlignmentCenter;
    titleLB.textColor=COLOR(44,146, 157);
    [headerView addSubview:titleLB];
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self disMissCurrentView];
    [self chooseMsgVisibleViewWithContent:[_titleArray objectAtIndex:indexPath.row]];
    
    
}
- (void)chooseMsgVisibleViewWithContent:(NSString *)content
{
    if ([_delegate respondsToSelector:@selector(chooseMsgVisibleViewWithContent:)]) {
        [_delegate chooseMsgVisibleViewWithContent:content];
    }
}

#pragma mark UITableViewDateSource--
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *vCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    vCell.backgroundColor=[UIColor clearColor];
    vCell.textLabel.textAlignment=NSTextAlignmentCenter;
    vCell.textLabel.textColor=COLOR(80, 80, 80);
    vCell.textLabel.font=[UIFont systemFontOfSize:14];
    vCell.textLabel.text=[_titleArray objectAtIndex:indexPath.row];
    
    if (indexPath.row!=_titleArray.count-1) {
        UIView *seperateView=[[UIView alloc] initWithFrame:CGRectMake(0, 39, vCell.frame.size.width, 0.5)];
        seperateView.backgroundColor=[UIColor lightGrayColor];
        seperateView.alpha=0.3;
        [vCell.contentView addSubview:seperateView];
    }
    
    return vCell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
